(function() {
  app.directive('toast', function() {
    return {
      restrict: 'E',
      templateUrl: 'public/directive/toast.html',
      scope: {},
      replace: true,
      controller: function($scope, toast_config, $state, $timeout) {
        $scope.ctrl = toast_config;
        $scope.state_go = $state;

      },
      link: function(scope, ele, atr) {
        var y = 0;
        ele.off();

        ele.on('click', function(event) {
          scope.state_go.go('vip-index');
        });

        ele.on('touchstart', function(event) {
          y = (event.touches[0].clientY - ($(this).offset().top - $(document).scrollTop()));
        });

        ele.on('touchmove', function(event) {
          var top = event.touches[0].clientY - y;
          if (top <= 50) top = 50;
          if (top >= $(window).height() - 100) top = $(window).height() - 100;
          $(this).css({ top: top + 'px' });
          event.preventDefault();
        });

        ele.on('touchend', function(event) {

        });
      }
    };
  });

  app.factory('toast', function(toast_config) {
    return {
      show: function(state) {
        toast_config.view = false;
        for (var j = 0; j < toast_config.page.length; j++) {
          if (toast_config.page[j] === state) {
            toast_config.view = true;
          }
        }
      },

      ifVip: function(bool,type) {
        if(type==='get'){
          return sessionStorage.getItem('vip_flag');
        }
        sessionStorage.setItem('vip_flag',bool);
        toast_config.toggle = bool;
        if(!bool){
          sessionStorage.removeItem('vip_account_index');
        }
      }
    };
  });
})();
